package com.module.admin.controller;

import com.module.admin.Warpper.UserOrderWarpper;
import com.module.common.factory.ConstantFactory;
import com.module.model.Dict;
import com.module.model.Order;
import com.module.model.OrderGoods;
import com.module.model.OrderInfo;
import com.module.service.OrderService;
import com.module.service.ShipAddressService;
import com.module.user.domain.BaseResp;
import com.module.utils.YuanFenConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 13192
 */

@Controller
@RequestMapping("userOrder")
public class UserOrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ShipAddressService shipAddressService;

    private static String PREFIX = "/back/system/order/";

    @RequestMapping("")
    public String orderIndex(){
        return PREFIX+"order.btl";
    }

    @RequestMapping("/order_info/{orderid}")
    public String orderInfo(Model model, @PathVariable Integer orderid){
        Order order = new Order();
        OrderInfo orderInfo = this.orderService.getOrderById(orderid);
        order.setId(orderInfo.getId());
        order.setOrderStatus(orderInfo.getOrderStatus());
        order.setPayType(orderInfo.getPayType());
        order.setTotalPrice(YuanFenConverter.changeY2F(Long.valueOf(orderInfo.getTotalPrice())));
        order.setShipAddress(this.shipAddressService.getShipAddressById(orderInfo.getShipId()));
        List<OrderGoods> orderGoodsList = this.orderService.getOrderGoodsList(orderInfo.getId());
        for (OrderGoods orderGoods : orderGoodsList) {
            orderGoods.setGoodsPrice(YuanFenConverter.changeY2F(orderGoods.getGoodsPrice()));
        }
        order.setOrderGoodsList(orderGoodsList);
        model.addAttribute("order",order);
        return PREFIX+"order_info.btl";
    }


    /**
     *更新订单状态
     */
    @RequestMapping("order_edit/{orderid}")
    public String orderStatus(Model model,@PathVariable Integer orderid){
        OrderInfo orderInfo = this.orderService.getOrderById(orderid);
        model.addAttribute("orderInfo", orderInfo);
        return PREFIX+"order_edit.btl";
    }
    /**
     *更新订单状态提交
     */
    @RequestMapping("order_editSubmit")
    public String orderStatusSubmit(Model model,@PathVariable Integer orderid){
//        OrderInfo orderInfo = this.orderService.getOrderById(orderid);
//        orderInfo.setOrderStatus(Integer.valueOf(5));
//        this.orderService.modifyOrder(orderInfo);
//        Dict dict = dictService.selectById(dictId);
//        model.addAttribute("dict", dict);
//        List<Dict> subDicts = ConstantFactory.me().findInDict(dictId);
//        model.addAttribute("subDicts", subDicts);

        return PREFIX+"order_edit.btl";
    }

    /**
     * @return
     * 获取订单详情信息
     */
    @RequestMapping(value = "/getUserOrderDetail/{orderid}")
    @ResponseBody
    public Order getUserOrderDetail(@PathVariable Integer orderid){
        Order order = new Order();
        OrderInfo orderInfo = this.orderService.getOrderById(orderid);
        order.setId(orderInfo.getId());
        order.setOrderStatus(orderInfo.getOrderStatus());
        order.setPayType(orderInfo.getPayType());
        order.setTotalPrice(YuanFenConverter.changeY2F(Long.valueOf(orderInfo.getTotalPrice())));
        order.setShipAddress(this.shipAddressService.getShipAddressById(orderInfo.getShipId()));
        List<OrderGoods> orderGoodsList = this.orderService.getOrderGoodsList(orderInfo.getId());
        for (OrderGoods orderGoods : orderGoodsList) {
            orderGoods.setGoodsPrice(YuanFenConverter.changeY2F(orderGoods.getGoodsPrice()));
        }
        order.setOrderGoodsList(orderGoodsList);

        return order;
    }
    /**
     * @return
     * 获取所有订单详情信息
     */
    @RequestMapping(value = "/getUserOrderListDetail")
    @ResponseBody
    public List<Order> getUserOrderListDetail(){
        List<OrderInfo> list = this.orderService.getUserOrderList();
        List orderList = new ArrayList();
        for (OrderInfo info : list) {
            Order order = new Order();
            order.setId(info.getId());
            order.setOrderStatus(info.getOrderStatus());
            order.setPayType(info.getPayType());
            order.setTotalPrice(YuanFenConverter.changeY2F(Long.valueOf(info.getTotalPrice())));
            order.setShipAddress(this.shipAddressService.getShipAddressById(info.getShipId()));
            List<OrderGoods> goodsList = this.orderService.getOrderGoodsList(info.getId());
            for (OrderGoods orderGoods : goodsList) {
                orderGoods.setGoodsPrice(YuanFenConverter.changeY2F(orderGoods.getGoodsPrice()));
            }
            order.setOrderGoodsList(goodsList);
            orderList.add(order);
        }
        return orderList;
    }
    /**
     * @return
     * 获取所有订单信息
     */
    @RequestMapping(value = "/getUserOrderList")
    @ResponseBody
    public Object getUserOrderList(){
        List<Map<String,Object>> list = this.orderService.getUserOrderMap();
        return new UserOrderWarpper(list).warp();
    }




}
